package com.lin.csdn.controller;

import com.lin.csdn.model.po.Messege;
import com.lin.csdn.service.impl.PicReportService;
import com.lin.csdn.utils.BaseLogger;
import net.sf.json.JSONObject;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.util.*;

@WebServlet("/PictureServlet")
public class PictureServlet extends HttpServlet {
    /**
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     *
     * 0：新增博客的图片 AddBlogPic
     * 1：新增举报用的图片或截图  AddReportPic
     *
     */
    BaseLogger baseLogger=new BaseLogger();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        //建立功能代表的code调用
        String AddBlogPic="0";//对博客新增图片
        String AddReportPic="1";//对举报系统新增图片
        //获取req数据
        String data = req.getParameter("picture");
        String imgData=data.replaceAll(" ","+");
        String base64Data =  imgData.split(",")[1];
        String code=req.getParameter("code");

        String url = null;
        Messege messege;
        PicReportService pictureService = new PicReportService();
        PrintWriter writer = resp.getWriter();
        JSONObject object = new JSONObject();

        //根据需求调用各自的方法
        if(code.equals(AddBlogPic)) {
            String blogId = req.getParameter("blogId");
            url = "G:\\WIS\\Wistudio\\web-app\\static\\picture\\blog" + blogId + ".jpg";
            String addPath = "..\\..\\..\\picture\\blog" + blogId + ".jpg";

            messege = pictureService.addPic(blogId, addPath);
            object.put("result", messege.getResult());
        }
        else if(code.equals(AddReportPic)){
            String reportId=req.getParameter("reportId");
            url = "G:\\WIS\\Wistudio\\web-app\\static\\picture\\report" + reportId + ".jpg";
            String addPath = "..\\..\\..\\picture\\report" + reportId + ".jpg";
            messege=pictureService.addReportPic(reportId,addPath);
            object.put("result", messege.getResult());
        }

            Base64.Decoder decoder = Base64.getDecoder();
            byte[] bytes = decoder.decode(base64Data);

            //字节流转文件

            FileOutputStream fos = null;
            try {
                assert url != null;
                fos = new FileOutputStream(url);
                fos.write(bytes);
            } catch (IOException e) {
                baseLogger.errorLog(String.valueOf(e));
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        baseLogger.errorLog(String.valueOf(e));
                    }
                }
            }
            writer.print(object);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }


}
